// Author: Domien Nowicki

#include "HighscoreDialog.h"
#include "Clock.h"

HighscoreDialog::HighscoreDialog(QWidget *parent): QDialog(parent)
{
	// Initialize UI
	setupUi();
}

HighscoreDialog::HighscoreDialog(const HighscoreDialog& rhs): Highscore(rhs)
{
	// Initialize UI
	setupUi();
	// Populate the table view with highscore entries
	Update();
}

HighscoreDialog::HighscoreDialog(QWidget *parent, const Highscore& rhs): QDialog(parent), Highscore(rhs)
{
	// Initialize UI
	setupUi();
	// Populate the table view with highscore entries
	Update();
}

void HighscoreDialog::setupUi()
{
	setWindowTitle("Highscores");
	setModal(true);

	gridLayout = new QGridLayout(this);
    gridLayout->setSpacing(6);
    gridLayout->setMargin(9);

	// Create a new table view
    tableView = new QTableView(this);
    tableView->setAlternatingRowColors(true);
	tableView->setSortingEnabled(false);

	// Create a new item model, and initialize its columns with: Name | Colors | Time | Steps
	model = new QStandardItemModel(0, 4, this);
	model->setHeaderData(0, Qt::Horizontal, QObject::tr("Name"));
    model->setHeaderData(1, Qt::Horizontal, QObject::tr("Colors"));
    model->setHeaderData(2, Qt::Horizontal, QObject::tr("Time"));
	model->setHeaderData(3, Qt::Horizontal, QObject::tr("Steps"));
	tableView->setModel(model);

    gridLayout->addWidget(tableView, 0, 0, 1, 1);

    buttonBox = new QDialogButtonBox(this);
    buttonBox->setOrientation(Qt::Horizontal);
    buttonBox->setStandardButtons(QDialogButtonBox::Ok);
    gridLayout->addWidget(buttonBox, 1, 0, 1, 1);

    QSize size(500, 300);
    size = size.expandedTo(this->minimumSizeHint());
    this->resize(size);

	// Connect signals
    QObject::connect(buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    QObject::connect(buttonBox, SIGNAL(rejected()), this, SLOT(reject()));
}

HighscoreDialog::~HighscoreDialog()
{
	
}





void HighscoreDialog::Update()
{
	// Clear all rows from the table
	model->removeRows(0, model->rowCount());

	// Items must be added in reverse to the table view
	std::vector<Score>::const_reverse_iterator it;

	// For each score in the highscores
	for(it = GetScores().rbegin(); it != GetScores().rend(); ++it)
	{
		const Score &score = *it;

		// Insert empty row
		model->insertRow(0);

		// Convert STL string to QString
		QString name = score.GetName().c_str();
		Clock clock(score.GetTime());
		QString time = clock.TimeToString().c_str();
		
		// Set table view column data
		model->setData(model->index(0, 0), name);
		model->setData(model->index(0, 1), score.GetColorCount());
		model->setData(model->index(0, 2), time);
		model->setData(model->index(0, 3), score.GetStepCount());

		// Make sure user can't edit the data
		model->itemFromIndex(model->index(0, 0))->setEditable(false);
		model->itemFromIndex(model->index(0, 1))->setEditable(false);
		model->itemFromIndex(model->index(0, 2))->setEditable(false);	
		model->itemFromIndex(model->index(0, 3))->setEditable(false);	
	}
}



